package com.huanxi.joytravel.ui;

import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.huanxi.joytravel.R;
import com.huanxi.joytravel.bases.BaseActivity;
import com.huanxi.joytravel.enums.MainChannel;
import com.huanxi.joytravel.fragments.HomeFragment;
import com.huanxi.joytravel.fragments.MineFragment;
import com.huanxi.joytravel.fragments.OrderFragment;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.frame_content)
    FrameLayout frameContent;
    @BindView(R.id.bottom_navigation_view)
    BottomNavigationView bottomNavigationView;
    private Fragment mCurrFragment;
    private Fragment mHomeFragment;
    private Fragment mOrderFragment;
    private Fragment mMineFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init() {
        initHomeView();
    }

    private void initHomeView() {
        if (mHomeFragment == null) {
            mHomeFragment = HomeFragment.getNewInstance();
        }
        if (mOrderFragment == null) {
            mOrderFragment = OrderFragment.getNewInstance();
        }
        if (mMineFragment == null) {
            mMineFragment = MineFragment.getNewInstance();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, mHomeFragment, MainChannel.HOME.getValue()).commit();
        bottomNavigationView.setItemIconTintList(null);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int i = menuItem.getItemId();
                if (i == R.id.navigation_home) {
                    switchContent(mCurrFragment, mHomeFragment, MainChannel.HOME.getValue());
                    mCurrFragment = mHomeFragment;

                    return true;
                } else if (i == R.id.navigation_order) {
                    switchContent(mCurrFragment, mOrderFragment, MainChannel.ORDER.getValue());
                    mCurrFragment = mOrderFragment;

                    return true;
                } else if (i == R.id.navigation_me) {
                    switchContent(mCurrFragment, mMineFragment, MainChannel.MINE.getValue());
                    mCurrFragment = mMineFragment;

                    return true;
                }
                return false;
            }
        });
    }

    public void switchContent(Fragment from, Fragment to, String tag) {
        if (from == null || to == null) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!to.isAdded()) {
            transaction.hide(from).add(R.id.frame_content, to, tag).commit();
        } else {
            transaction.hide(from).show(to).commit();
        }
    }
}